﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PilotProject;
using PilotMVC.Models;

namespace PilotMVC.Controllers
{
    //[Authorize(Roles = "Admin")]
    public class CandidateController : Controller
    {
        private PilotEntities1 db = new PilotEntities1();

        
        public ActionResult Index(string jpa, string firstName, string lastName)
        {
            var candidates = from candidate in db.People.OfType<Candidate>() select candidate;
            var candidateView = new ManageCandidateView();

            if (!string.IsNullOrEmpty(jpa) && !jpa.Equals(""))
            {
                candidateView.Candidates = db.People.Include("Address").OfType<Candidate>().Where(e => e.JPA.Contains(jpa)).ToList();
            }
            if (!string.IsNullOrEmpty(firstName) && !firstName.Equals(""))
            {
                candidateView.Candidates = db.People.Include("Address").OfType<Candidate>().Where(e => e.FirstName.ToLower().Contains(firstName.ToLower())).ToList();
            }
            if (!string.IsNullOrEmpty(lastName) && !lastName.Equals(""))
            {
                candidateView.Candidates = db.People.Include("Address").OfType<Candidate>().Where(e => e.LastName.ToLower().Contains(lastName.ToLower())).ToList();
            }


            return View(candidateView);
        }

        //
        // GET: /Candiate/Create

        public ActionResult Create()
        {
            Candidate candidate = new Candidate();
            candidate.DateOfBirth = DateTime.Now;
            return View(candidate);
        }

        //
        // POST: /Candiate/Create

        [HttpPost]
        public ActionResult Create(Candidate candidate, FormCollection formCollection)
        {
            if (ModelState.IsValid)
            {
                var address = new Address();
                address.Id = Guid.NewGuid();
                address.SuburbId = candidate.Address.SuburbId;
                address.Street1 = candidate.Address.Street1;
                address.Street2 = candidate.Address.Street2;
                db.Addresses.AddObject(address);
   

                var person = new Candidate();
                person.Id = Guid.NewGuid();
                person.DateOfBirth = candidate.DateOfBirth;
                person.Address = address;
                person.ARN = candidate.ARN;
                person.Email = candidate.Email;
                person.FirstName = candidate.FirstName;
                person.JPA = candidate.JPA;
                person.LastName = candidate.LastName;
                person.Telephone = candidate.Telephone;
                person.Mobile = candidate.Mobile; 
       
                
                db.People.AddObject(person);                
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(candidate);
        }

        //
        // GET: /Candiate/Edit/5

        public ActionResult Edit(Guid id)
        {
            Candidate candidate = db.People.OfType<Candidate>().Include("Address").Single(c => c.Id == id);
            return View(candidate);
        }

        //
        // POST: /Candiate/Edit/5
        [HttpPost]
        public ActionResult Edit(Candidate candidate, FormCollection data, String DateOfBirth)
        {
            
            ModelState.Remove("States");
            if (ModelState.IsValid)
            {
                var person = db.People.Where(p => p.Id == candidate.Id).First();
                person.Address.SuburbId = candidate.Address.SuburbId;
                person.Address.Street1 = candidate.Address.Street1;
                person.Address.Street2 = candidate.Address.Street2;
                person.Email = candidate.Email;
                person.FirstName = candidate.FirstName;
                person.DateOfBirth = candidate.DateOfBirth;
                person.LastName = candidate.LastName;
                person.Mobile = candidate.Mobile;
                person.Telephone = candidate.Telephone;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.AddressId = new SelectList(db.Addresses, "Id", "Street1", candidate.AddressId);
            return View(candidate);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}